package io.github.wanggit.antrpc.commons.codec.serialize;

import io.github.wanggit.antrpc.commons.codec.serialize.json.JsonSerializer;
import io.github.wanggit.antrpc.commons.codec.serialize.kryo.KryoSerializer;
import io.github.wanggit.antrpc.commons.constants.ConstantValues;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class SerializerFactory implements ISerializerFactory {

    private static final ISerializerFactory instance = new SerializerFactory();

    private final Map<Byte, ISerializer> serializerMap = new HashMap<>();

    private SerializerFactory() {
        ISerializer kryoSerializer = new KryoSerializer();
        kryoSerializer.setConfigs(new HashMap<>());
        kryoSerializer.init();
        serializerMap.put(ConstantValues.KRYO_SERIALIZER, kryoSerializer);

        ISerializer jsonSerializer = new JsonSerializer();
        jsonSerializer.setConfigs(new HashMap<>());
        jsonSerializer.init();
        serializerMap.put(ConstantValues.JSON_SERIALIZER, jsonSerializer);
    }

    public static ISerializerFactory getInstance() {
        return instance;
    }

    @Override
    public ISerializer getDefaultConfigsSerializerByByteCmd(byte cmd) {
        ISerializer serializer = serializerMap.get(cmd);
        if (null == serializer && cmd != ConstantValues.DEFAULT_SERIALIZER && log.isWarnEnabled()) {
            log.warn("Can not found Serializer by cmd + " + cmd);
        }
        return serializer;
    }
}
